﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace XrmLibrary.Extensions
{
    public static class SqlDataReaderExtensions
    {
        /// <summary>
        /// Gets a value from the SqlDataReader column or returns the default value of type parameter if the type is inconsistent.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="reader">The reader.</param>
        /// <param name="columnName">Name of the column.</param>
        /// <returns></returns>
        public static T GetColumnOrDefault<T>(this SqlDataReader reader, string columnName)
        {
            Object obj = reader[columnName];
            if (obj is T)
            {
                return (T) obj;
            }
            else
            {
                return default(T);
            }
        }
    }
}
